//
//  IDAuthView.m
//  身份证识别
//

//  Created by runone02 on 2018/9/4.
//  Copyright © 2018年 liqiyun. All rights reserved.
//

#import "IDAuthView.h"
#import "AVCaptureViewController.h"
@interface IDAuthView ()
/** */
@property (nonatomic,strong) UILabel *titleLabel;
/**图片 */
@property (nonatomic,strong) UIImageView *image;
/**开始识别 */
@property (nonatomic,strong) UIButton *recognitionButton;
/** */
@property (nonatomic,strong) UILabel *idLabel;
/**号码*/
@property (nonatomic,strong) UILabel *IDNumLabel;
/**重拍 */
@property (nonatomic,strong) UIButton *swatButton;
/**下一步 */
@property (nonatomic,strong) UIButton *nextButton;
@property (nonatomic,strong) UILabel *num; //身份证号
@property (nonatomic,strong) UILabel *name; //姓名
@property (nonatomic,strong) UILabel *gender; //性别
@property (nonatomic,strong) UILabel *nation; //民族
@property (nonatomic,strong) UILabel *address; //地址
@end


@implementation IDAuthView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        [self setupUI];
    }
    return self;
}

- (void)setupUI {
    self.backgroundColor = [UIColor whiteColor];
    self.titleLabel = [[UILabel alloc]init];
    self.titleLabel.text = @"请准备好您的身份证原件";
    self.titleLabel.textColor = [UIColor lightGrayColor];
    self.titleLabel.font = TITLE_FONT(30);
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.titleLabel];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.left.right.mas_equalTo(self);
        make.height.mas_equalTo(15);
        make.top.mas_equalTo(self).with.offset(15);
    }];
    
    self.image = [[UIImageView alloc]init];
    self.image.image = IMAGE(@"idcard_first");
    [self addSubview:self.image];
    
    [self.image mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.titleLabel.mas_bottom).with.offset(20);
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.height.mas_equalTo(200);
    }];

    self.idLabel = [[UILabel alloc]init];
    self.idLabel.text = @"请核对身份证号码，确认无误";
    self.idLabel.font = TITLE_FONT(30);
    self.idLabel.hidden = YES;
    self.idLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.idLabel];
    
    [self.idLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.image.mas_bottom).with.offset(20);
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.height.mas_equalTo(20);
    }];
    
    self.name = [[UILabel alloc]init];
    self.name.hidden = YES;
    self.name.textColor = RGBA(49, 133, 193, 1);
    self.name.font = TITLE_FONT(30);
    self.name.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.name];
    
    [self.name mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.idLabel.mas_bottom).with.offset(5);
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.height.mas_equalTo(15);
    }];
    
    self.gender = [[UILabel alloc]init];
    self.gender.hidden = YES;
    self.gender.textColor = RGBA(49, 133, 193, 1);
    self.gender.font = TITLE_FONT(30);
    self.gender.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.gender];
    
    [self.gender mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.name.mas_bottom).with.offset(5);
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.height.mas_equalTo(15);
    }];
    
    self.nation = [[UILabel alloc]init];
    self.nation.hidden = YES;
    self.nation.textColor = RGBA(49, 133, 193, 1);
    self.nation.font = TITLE_FONT(30);
    self.nation.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.nation];
    
    [self.nation mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.gender.mas_bottom).with.offset(5);
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.height.mas_equalTo(15);
    }];
    
    self.address = [[UILabel alloc]init];
    self.address.hidden = YES;
    self.address.textColor = RGBA(49, 133, 193, 1);
    self.address.font = TITLE_FONT(30);
    self.address.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.address];
    
    [self.address mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.nation.mas_bottom).with.offset(5);
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.height.mas_equalTo(15);
    }];

    self.IDNumLabel = [[UILabel alloc]init];
    self.IDNumLabel.hidden = YES;
    self.IDNumLabel.textColor = RGBA(49, 133, 193, 1);
    self.IDNumLabel.font = TITLE_FONT(30);
    self.IDNumLabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:self.IDNumLabel];
    
    [self.IDNumLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.address.mas_bottom).with.offset(5);
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.height.mas_equalTo(30);
    }];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = [UIColor whiteColor];
    [self addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self);
        make.top.mas_equalTo(self.IDNumLabel.mas_bottom);
        make.bottom.mas_equalTo(self);
        make.width.mas_equalTo(1);
    }];
    
    self.swatButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.swatButton setTitle:@"错误，重新拍" forState:UIControlStateNormal];
    [self.swatButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.swatButton.backgroundColor = [UIColor orangeColor];
    [ self.swatButton.layer setMasksToBounds:YES];
    [ self.swatButton.layer setCornerRadius:5.0];
    
    [ self.swatButton.layer setBorderWidth:1.0];
    self.swatButton.layer.borderColor = [UIColor orangeColor].CGColor;
    [self.swatButton addTarget:self action:@selector(shootAgain:) forControlEvents:UIControlEventTouchUpInside];
    self.swatButton.hidden = YES;
    [self addSubview:self.swatButton];
    
    [self.swatButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.IDNumLabel.mas_bottom).with.offset(20);
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(line).with.offset(-20);
        make.height.mas_equalTo(30);
    }];
    
    self.nextButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.nextButton setTitle:@"正确，下一步" forState:UIControlStateNormal];
    [self.nextButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.nextButton.backgroundColor = [UIColor orangeColor];
    [ self.nextButton.layer setMasksToBounds:YES];
    [ self.nextButton.layer setCornerRadius:5.0];
    [ self.nextButton.layer setBorderWidth:1.0];
    self.nextButton.layer.borderColor = [UIColor orangeColor].CGColor;
    [self.nextButton addTarget:self action:@selector(nextStep:) forControlEvents:UIControlEventTouchUpInside];
    self.nextButton.hidden = YES;
    [self addSubview:self.nextButton];
    
    [self.nextButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.IDNumLabel.mas_bottom).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.left.mas_equalTo(line).with.offset(20);
        make.height.mas_equalTo(30);
    }];
    
    self.recognitionButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.recognitionButton setTitle:@"开始识别" forState:UIControlStateNormal];
    [self.recognitionButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    self.recognitionButton.backgroundColor = [UIColor orangeColor];
    [self.recognitionButton.layer setMasksToBounds:YES];
    [self.recognitionButton.layer setCornerRadius:5.0];
    [self.recognitionButton.layer setBorderWidth:1.0];
    [self.recognitionButton addTarget:self action:@selector(recognitionButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    self.recognitionButton.layer.borderColor = [UIColor orangeColor].CGColor;
    
    [self addSubview:self.recognitionButton];
    
    [self.recognitionButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.image.mas_bottom).with.offset(30);
        make.left.mas_equalTo(self).with.offset(20);
        make.right.mas_equalTo(self).with.offset(-20);
        make.height.mas_equalTo(50);
    }];
}


- (void)recognitionButtonClick:(UIButton *)btn{
    WEAKSELF
    AVCaptureViewController *AVCaptureVC = [[AVCaptureViewController alloc] init];
    AVCaptureVC.idInfoBlock = ^(IDInfo *idInfo, UIImage *image) {
        weakSelf.idLabel.hidden = NO;
        weakSelf.IDNumLabel.hidden = NO;
        weakSelf.titleLabel.text = @"您的身份证";
        weakSelf.swatButton.hidden = NO;
        weakSelf.nextButton.hidden = NO;
        weakSelf.recognitionButton.hidden = YES;
        weakSelf.name.hidden = NO;
        weakSelf.gender.hidden = NO;
        weakSelf.nation.hidden = NO;
        weakSelf.address.hidden = NO;
        
        
        weakSelf.IDNumLabel.text = idInfo.num;
        weakSelf.name.text = [NSString stringWithFormat:@"名字: %@",idInfo.name];
        weakSelf.gender.text = [NSString stringWithFormat:@"性别: %@",idInfo.gender];
        weakSelf.nation.text = [NSString stringWithFormat:@"民族: %@",idInfo.nation];
        weakSelf.address.text = [NSString stringWithFormat:@"地址: %@",idInfo.address];
        
        weakSelf.image.image = image;
    };
    [self.viewController.navigationController pushViewController:AVCaptureVC animated:YES];
}

- (void)shootAgain:(UIButton *)btn{
    WEAKSELF
    [self recognitionButtonClick:weakSelf.recognitionButton];
}

- (void)nextStep:(UIButton *)btn{
     NSLog(@"经用户核对，身份证号码正确，那就进行下一步，比如身份证图像或号码经加密后，传递给后台");
}
@end
